<style>
    .table > tbody > tr > td > label.asterisk:after {
        content: " *";
        color: red;
    }
    .form-copy {
        width: 200px;
        margin-right: 5px;
        border-radius: 0;
        box-shadow: none;
        padding: 4px 12px 7px 12px;
        font-size: 14px;
        line-height: 1.42857143;
        color: #555;
        border: 1px solid #ccc;
        transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    }
</style>
<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <h6 style="font-weight: bolder">仓库信息</h6>
        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right;width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">平台</label>
                </td>
                <td style="text-align: left;width: 500px;">
                    <select id="plat_id" name="plat_id" required>
                        {{ getOptions(\App\Models\Basics\SysPlat::options(), $w->plat_id) }}
                    </select>&nbsp;&nbsp;
                    <span style="margin-top: 5px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;选择海外仓平台(默认平台：Amazon)</span>
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>
        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right;width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">海外仓</label>
                </td>
                <td style="text-align: left;width: 500px;">
                    <select id="group_id" name="group_id" required>
                        {{ getOptions(\App\Models\OverSeasBa\WarehouseGroup::options(),$w->group_id) }}
                    </select>&nbsp;&nbsp;
                    <span style="margin-top: 5px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;若海外仓不存在请先<a href="/xqc/overseas-ba/warehouse-group">添加</a></span>
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>
        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right;width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">名称</label>
                </td>
                <td style="text-align: left;width: 500px;">
                    <input type="text" class="form-copy" name="w_code" value="{{ $w->w_code }}" placeholder="名称">
                    <span style="margin-top: 5px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;建议编码模式命名，例：USxx001</span>
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>
        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right;width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">国家</label>
                </td>
                <td style="text-align: left;width: 500px;">
                    <select id="r_country_id" name="country_id" required>
                        {{ getOptions(\App\Models\Basics\AmazonCountry::options(),$w->country_id) }}
                    </select>&nbsp;&nbsp;
                    <span style="margin-top: 5px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;库存会关联到对应国家所在站点</span>
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>
        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right;width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">入库代码</label>
                </td>
                <td style="text-align: left;width: 500px;">
                    <input type="text" class="form-copy" name="in_code" value="{{ $w->in_code }}" placeholder="入库代码">
                    <span style="margin-top: 5px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;若无入库代码可用国家编码代替，例：US</span>
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>
        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right; width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">仓库功能</label>
                </td>
                <td style="text-align: left; padding-top: 10px;">
                    <?php $modelArray = $w->warehouse_model ?? []; ?>
                    @foreach(\App\Models\OverSeasBa\Warehouse::$_warehouse as $warehouseId => $title)
                        {{ $title }} <input class="grid-row-checkbox" type="checkbox" name="warehouse_model[]" <?= in_array($warehouseId, $modelArray) ? 'checked':'';?> value="{{ $warehouseId }}"> &nbsp;
                    @endforeach
                    <span style="margin-top: 5px;margin-left:20px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;请选择仓库功能</span>
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>

        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right; width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">仓库服务</label>
                </td>
                <td style="text-align: left; padding-top: 10px;">
                    <?php $serviceArray = $w->warehouse_service ?? []; ?>
                    @foreach(\App\Models\OverSeasBa\Warehouse::$_warehouse as $warehouseId => $title)
                        {{ $title }} <input class="grid-row-checkbox" type="checkbox" name="warehouse_service[]" <?= in_array($warehouseId, $serviceArray) ? 'checked':'';?> value="{{ $warehouseId }}"> &nbsp;
                    @endforeach
                    <span style="margin-top: 5px;margin-left:20px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;请选择仓库服务（合作服务）</span>
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>
        <table class="table">
            <tbody>
            <tr>
                <td style="text-align: right;width: 100px;">
                    <label class="asterisk" style="padding-top: 8px;">仓库状态</label>
                </td>
                <td style="text-align: left;width: 500px;">
                    <select id="r_status" name="status" required>
                        {{ getOptions(\App\Models\OverSeasBa\Warehouse::$_status,$w->status) }}
                    </select>&nbsp;&nbsp;
                </td>
                <td></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <h6 style="font-weight: bolder">仓库信息</h6>
        <div class="box-body table-responsive no-padding">
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;">联系人</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy" name="contact" value="{{ $w->contact }}" placeholder="联系人">
                    </td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;">地址</label>
                    </td>
                    <td colspan="3" style="text-align: left;">
                        <input type="text" class="form-copy" style="width: 520px;" name="address" value="{{ $w->address }}" placeholder="地址">
                    </td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;">邮编</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <input type="text" class="form-copy" name="postcode" value="{{ $w->postcode }}" placeholder="邮编">
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;" >电话</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy" name="phone" value="{{ $w->phone }}" placeholder="电话">
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="box-footer">
    {{ csrf_field() }}
    <input type="hidden" name="id" value="{{ $w->id }}">
    <div class="col-md-3"></div>
    <div class="col-md-5" style="text-align: center;">
        <div class="btn-group" style="margin-right: 10%">
            <button type="reset" class="btn btn-warning">重置</button>
        </div>
        <div class="btn-group">
            <button type="button" id="owc-form" class="btn btn-primary">提交</button>
        </div>
    </div>
</div>
<script language="JavaScript" type="text/javascript">
    function checkForm(){
        let platId = $('#plat_id').val();
        if(platId == ''){
            swal('平台不能为空', '', 'error');
            return false;
        }
        let groupId = $('#group_id').val();
        if(groupId == ''){
            swal('海外仓不能为空', '', 'error');
            return false;
        }
        let wCode = $('input[name="w_code"]').val();
        if(wCode.trim() == ''){
            swal('名称不能为空', '', 'error');
            return false;
        }
        let inCode = $('input[name="in_code"]').val();
        if(inCode.trim() == ''){
            swal('入库代码不能为空', '', 'error');
            return false;
        }
        let countryId = $('#r_country_id').val();
        if(countryId == ''){
            swal('国家不能为空', '', 'error');
            return false;
        }
        let status = $('#r_status').val();
        if(status == ''){
            swal('状态不能为空', '', 'error');
            return false;
        }
        let contact = $('input[name="contact"]').val();
        if(contact.trim() == ''){
            swal('联系人不能为空', '', 'error');
            return false;
        }
        let address = $('input[name="address"]').val();
        if(address.trim() == ''){
            swal('地址不能为空', '', 'error');
            return false;
        }
        let postcode = $('input[name="postcode"]').val();
        if(postcode.trim() == ''){
            swal('邮编不能为空', '', 'error');
            return false;
        }
        let phone = $('input[name="phone"]').val();
        if(phone.trim() == ''){
            swal('电话不能为空', '', 'error');
            return false;
        }
        return true;
    }
    $(function () {

        $('.grid-row-checkbox').iCheck({checkboxClass:'icheckbox_minimal-blue'})
        $('#plat_id').select2({placeholder: '平台', allowClear: true, width: '200px'});
        $('#group_id').select2({placeholder: '海外仓', allowClear: true, width: '200px'});
        $('#r_country_id').select2({placeholder: '国家', allowClear: true, width: '200px'});
        $('#r_status').select2({placeholder: '启用', allowClear: true, width: '200px'});

        let getParam = function () {
            let param = {};
            param['id'] = $('input[name="id"]').val();
            param['plat_id'] = $('#plat_id').val();
            param['group_id'] = $('#group_id').val();
            param['w_code'] = $('input[name="w_code"]').val().trim().toUpperCase();
            param['in_code'] = $('input[name="in_code"]').val().trim().toUpperCase();
            param['country_id'] = $('#r_country_id').val();
            param['status'] = $('#r_status').val();
            param['contact'] = $('input[name="contact"]').val().trim();
            param['address'] = $('input[name="address"]').val().trim();
            param['postcode'] = $('input[name="postcode"]').val().trim();
            param['phone'] = $('input[name="phone"]').val().trim();
            return param;
        };

        $('#owc-form').on('click', function(e) {
            e.preventDefault();
            if(checkForm()){
                let param = getParam();

                //仓库功能checkbox
                let warehouseModel = [];
                $('input[name="warehouse_model[]"]:checked').each(function(){
                    warehouseModel.push($(this).val());
                });
                param['warehouse_model'] = warehouseModel;
                if(param['warehouse_model'].length == 0) {
                    swal('仓库功能不能为空', '', 'error');
                    return false;
                }
                //仓库服务checkbox
                let warehouseService = [];
                $('input[name="warehouse_service[]"]:checked').each(function(){
                    warehouseService.push($(this).val());
                });
                param['warehouse_service'] = warehouseService;
                if(param['warehouse_service'].length == 0) {
                    swal('仓库服务不能为空', '', 'error');
                    return false;
                }

                $.ajax({
                    method: 'post',
                    url: '/xqc/overseas-ba/w-ajax',
                    data: {
                        _token:"{{ csrf_token() }}",
                        action:'doEdit',
                        param:param
                    },
                    success: function (data) {
                        if (typeof data === 'object') {
                            if (data.status) {
                                $.pjax.reload('#warehouse-edit-form');
                                toastr.success('提交成功 !');
                            } else {
                                swal(data.message, '', 'error');
                            }
                        }
                    }
                });
            }
        });
    });
</script>
